package mathwork;

import java.util.Vector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class Poly extends Vector<Double> {
		
	private static final long serialVersionUID = 1L;

	private Double lsmR=null; 
	public Double PolyValue(Double x){
			int i=0;
			Double result=0.0;
			
			if(super.size()==0)
				return null;
			
			for(i=0;i<super.size();i++){
				result+=super.elementAt(i)*Math.pow(x, i);
			}
			
			return result;
		}
	
	public Double GetlsmR(){	return this.lsmR; }
	public void SetlsmR(Double val){	lsmR=val; }
	
	public String GetString(){
			int i=0;
			String result="y=";
			for(i=super.size()-1;i>=0;i--){
				if(i!=0){
					if(super.elementAt(i).compareTo(0.0)==0)
						continue;
					result+=super.elementAt(i)+"x^"+i;
					if(i!=0 && super.elementAt(i-1)>0)
						result+="+";
				}else{
					if(super.elementAt(i).compareTo(0.0)==0)
						continue;
					result+=super.elementAt(i);
				}
			}
			return result;
		}
		
		public void PolyPlot(Vector <Double> x){
			if(super.isEmpty() || x.isEmpty())
				return;
			
			XYSeries dataseries=new XYSeries(this.GetString());
			int i;
			for(i=0;i<x.size();i++){
				dataseries.add(x.elementAt(i), this.PolyValue(x.elementAt(i)));
			}
			
			XYSeriesCollection dataset=new XYSeriesCollection();
			dataset.addSeries(dataseries);
			
			JFreeChart chart=ChartFactory.createXYLineChart("Poly", "x", "y", dataset, PlotOrientation.HORIZONTAL, true, false, false);
			
			ChartFrame frame=new ChartFrame(this.GetString(),chart);
			
			frame.pack();
			frame.setVisible(true);
			
		}
}
